package gui_classes;

import java_persistence.AppointmentMapper;
import java_persistence.UserMapper;

import javax.swing.*;

import org.apache.ibatis.session.SqlSession;

import domain.Appointment;
import domain.Doctor;
import domain.Patient;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Iterator;
import java.util.List;

import domain.*;
import java_persistence.*;

/*
 * This class represents a patient appointment request screen
 */
public class PatientAppointmentRequestPanel extends DERPBase {
	private Patient pat;
	
//	private JTextField textField;
	private JComboBox doctorField;
	private JTextField txtYyyymmdd;
	private JTextField txtHhmmss;
	private JTextArea textArea;

	
	private JLabel lblTreatmentRecord;
	private JButton btnSubmit;
	private String[] arr;
	
	private List<Doctor> doctors;
//	private Doctor reqDoc;
	/**
	 * Create the panel.
	 */
	public PatientAppointmentRequestPanel(Patient pat) {
		setLayout(null);
		setPreferredSize(new Dimension(450, 350));
		
		this.pat = pat;
		
		JLabel lblNewLabel = new JLabel("Doctor");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblNewLabel.setBounds(10, 39, 46, 14);
		add(lblNewLabel);
		
		/*change for sublcass treatmentscreen */
		lblTreatmentRecord = new JLabel("Request Appointment");
		lblTreatmentRecord.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblTreatmentRecord.setVerticalAlignment(SwingConstants.TOP);
		lblTreatmentRecord.setBounds(163, 11, 177, 14);
		add(lblTreatmentRecord); 
		
		JLabel lblDate = new JLabel("Date");
		lblDate.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblDate.setBounds(10, 64, 46, 14);
		add(lblDate);
		
		JLabel lblTime = new JLabel("Time");
		lblTime.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblTime.setBounds(10, 89, 46, 14);
		add(lblTime);
		
		/*
		 * 
		 * This is a temp comment uncomment this VITAL piece of code once finished with treatment screen. */
		
		doctors = GUI_Main.getSession().getMapper(DoctorMapper.class).get_doctors();

		int i=0;
		String[] docStr = new String[doctors.size()];
		for ( Iterator<Doctor> doctorsIter = doctors.iterator(); doctorsIter.hasNext(); ) {
			Doctor curd = doctorsIter.next();
			docStr[i]=curd.getName() + " " + curd.getUsername();
			i++;
		}
		
		doctorField = new JComboBox(docStr);
		doctorField.setBounds(67, 39, 86, 20);
		add(doctorField);   
		
		
		txtYyyymmdd = new JTextField();
		txtYyyymmdd.setText("YYYY-MM-DD");
		txtYyyymmdd.setBounds(67, 64, 86, 20);
		add(txtYyyymmdd);
		txtYyyymmdd.setColumns(10);
		
		txtHhmmss = new JTextField();
		txtHhmmss.setText("HH:MM:SS");
		txtHhmmss.setBounds(67, 89, 86, 20);
		add(txtHhmmss);
		txtHhmmss.setColumns(10);
		
	
		JLabel lblComplaints = new JLabel("Complaints");
		lblComplaints.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblComplaints.setBounds(197, 64, 86, 14);
		add(lblComplaints);
		
		textArea = new JTextArea();
		textArea.setBounds(286, 59, 154, 47);
		add(textArea);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(163, 184, 117, 25);
		btnSubmit.addActionListener(new SubmitListener());
		add(btnSubmit);


	}
	
	/*
	 * setter for Title
	 * @param title
	 */
	public void setTitle(String title)
	{
		this.lblTreatmentRecord.setText(title);
	}
	
	
	/*
	 * removes button
	 */
	public void removeButton()
	{
		remove(btnSubmit);
	}
	
	/*
	 * getter for textfield
	 * @return textField
	 */
	public JComboBox getDoctorBox()
	{
		return this.doctorField;
	}
	
	/*
	 * getter for year date textfield
	 * @return txtYyymmdd
	 */
	public JTextField getTextField1()
	{
		return this.txtYyyymmdd;
	}
	
	public void setDate(String t)
	{
		this.txtYyyymmdd.setText(t);
	}
	
	
	/*
	 * getter for time textfield
	 * @return txtHhmmss
	 */
	public JTextField getTextField2()
	{
		return this.txtHhmmss;
	}
	
	
	public void setTime(String t)
	{
		this.txtHhmmss.setText(t);
	}
	
	public JTextField getTimeField()
	{
		return this.txtHhmmss;
	}
	
	/*
	 * getter for text area
	 * @return textArea
	 */
	public JTextArea getTextArea()
	{
		return this.textArea;
	}
	
	public void setTextArea(String t)
	{
		this.textArea.setText(t);
	}
	
	/*
	 * setter for array
	 * @param array The array to set to
	 */
	public void setArray(String[] array)
	{
		this.arr = array;
	}
	
	/*
	 * getter for array
	 * @return arr
	 */
	public String[] getArray()
	{
		return this.arr;
	}

	/*
	 * getter for patient
	 * @return pat
	 */
	public Patient getPatient()
	{
		return this.pat;
	}
	
	
	public void display() {

		GUI_Main.replacePanel(this);
	}
	
	/*
	 * Listener for Submit button
	 */
	private class SubmitListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			String[] myArray = new String[3];
			
			String date = GUI_Main.appointpage.getTextField1().getText() + " " +  GUI_Main.appointpage.getTextField2().getText();
		//	myArray[2] = (String) GUI_Main.appointpage.getDoctorBox().getSelectedItem(); // Doctor
			String[] dnames = ((String) GUI_Main.appointpage.getDoctorBox().getSelectedItem()).split(" "); // Doctor
			String du = dnames[dnames.length-1];
			myArray[2] = du;
			myArray[0] = GUI_Main.appointpage.getTextArea().getText(); // complaints
			myArray[1] = date; //date
			GUI_Main.appointpage.setArray(myArray);
			
			System.out.println("Set appointment");
			System.out.println(GUI_Main.appointpage.getArray()[0] + "\n" +GUI_Main.appointpage.getArray()[1] + "\n" + GUI_Main.appointpage.getArray()[2]);
			
			/*Joseph: GUI_Main.appointpage.getArray() will give you an array that has
			 *  at index 0: Complaint
			 *  at index 1: Date, in proper format
			 *  at index 2: requested Doctor
			 *  
			 *  Can you please commit this appointment to the database?
			 */
			
			SqlSession session = GUI_Main.getSession();
			
			/*
			 * Pratik: I need to know which patient i'm assigning the appointment to.
			 * 
			 * It is neither possible nor useful to save the appointment without knowing the patient.
			 * Edit the patient declaration below, and everything else will work.  As it is,
			 * it'll probably throw a null pointer exception. 
			 * 
			 * I think I fixed it. Let me test it.
			 *  
			 */
			
			System.out.println("Problem here");
			System.out.println(GUI_Main.appointpage.getPatient().getName());
			String patient_username = GUI_Main.appointpage.getPatient().getUsername();
			
			System.out.println(patient_username);
			
			// change myarray to using get
			session.getMapper(AppointmentMapper.class).create(myArray[1], myArray[0], myArray[2], patient_username);
			session.commit();
			
			/*
			 * At this point, the appointment will be saved into the database, which you can retrieve using AppointmentMapper.
			 * It's not stored in the application as an object, since this method wouldn't send it anywhere and Java's
			 * garbage collector would just delete it as soon as this method finishes. 
			 * 
			 */
			
			// Update PatientsPageTwoPanel
		//	newp = new PatientsPageTwoPanel
			
			//return to patient's page
			Patient pat = GUI_Main.ppage.getPanelTwo().getPatient();
		//	GUI_Main.appointpage.displayPrev();
			GUI_Main.ppage = new PatientPage(pat);
			GUI_Main.ppage.display();
			
			//GUI_Main.replacePanel(GUI_Main.frame, GUI_Main.ppage);
		}
	}
}
